package com.leetcode.链表操作.两数相加;

import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 参考：https://leetcode-cn.com/problems/add-two-numbers/
 *
 * TODO 注：LinkedList类中有内部类Node，其也是直接调用内部类Node的成员变量来做处理的。
 *
 * </p>
 *
 * @author: Sunny
 * @date: 2021/3/10
 * @version: v1.0.0
 */
public class Main {

    public static void main(String[] args) {
        ListNode node1 = new ListNode(2);
        ListNode node2 = new ListNode(4);
        node1.next = node2;
        ListNode node3 = new ListNode(3);
        node2.next = node3;

        ListNode node4 = new ListNode(5);
        ListNode node5 = new ListNode(6);
        node4.next = node5;
        ListNode node6 = new ListNode(4);
        node5.next = node6;

        Main main = new Main();
        System.out.println(main.addTwoNumbers(node1, node4));
        System.out.println(main.addTwoNumbers1(node1, node4));
    }


    /**
     * 两次遍历
     * 第一次遍历：两个链表对应每个节点分别取和，若含有空节点则空节点取0，产生一个新链表。
     * 第二次遍历：对取完和的新链表遍历，判断当前的val是否大于等于10，大于或等于则其自身-10其next加1，若next为空则新建0节点。
     */
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode rs = new ListNode(l1.val + l2.val);

        l1 = l1.next;
        l2 = l2.next;

        ListNode tmp = rs;
        while (l1 != null || l2 != null) {
            int a = 0;
            int b = 0;
            if (l1 != null) {
                a = l1.val;
            }
            if (l2 != null) {
                b = l2.val;
            }
            int t = a + b;
            tmp.next = new ListNode(t);
            tmp = tmp.next;
            if (l1 != null) {
                l1 = l1.next;
            }
            if (l2 != null) {
                l2 = l2.next;
            }
        }

        tmp = rs;
        while (tmp != null) {
            if (tmp.val >= 10) {
                tmp.val = tmp.val - 10;
                if (tmp.next == null) {
                    tmp.next = new ListNode(0);
                }
                tmp.next.val = tmp.next.val + 1;
            }
            tmp = tmp.next;
        }
        return rs;
    }


    /**
     * 两数相加
     * 自己写的，比较暴力
     *
     * @return
     */
    public ListNode addTwoNumbers1(ListNode l1, ListNode l2) {
        StringBuffer buffer1 = new StringBuffer();
        while (true) {
            buffer1.append(l1.val);
            l1 = l1.next;
            if (l1 == null) {
                break;
            }
        }
        buffer1 = buffer1.reverse();

        StringBuffer buffer2 = new StringBuffer();
        while (true) {
            buffer2.append(l2.val);
            l2 = l2.next;
            if (l2 == null) {
                break;
            }
        }
        buffer2 = buffer2.reverse();

        String result = String.valueOf(Integer.parseInt(buffer1.toString()) + Integer.parseInt(buffer2.toString()));

        StringBuffer buffer = new StringBuffer(result);
        buffer = buffer.reverse();
        // 708
        System.out.println(buffer.toString());

        List<ListNode> listNodes = new LinkedList<>();
        for (int i = 0; i < buffer.length(); i++) {
            String value = buffer.substring(i, i + 1);
            listNodes.add(new ListNode(Integer.parseInt(value)));
        }
        // 7 --- 0 --- 8
        for (int i = 0; i < listNodes.size(); i++) {
            ListNode current = listNodes.get(i);
            ListNode next = null;
            if (i + 1 < listNodes.size()) {
                next = listNodes.get(i + 1);
            } else {
                next = null;
            }
            current.next = next;
        }
        return listNodes.get(0);
    }

}
